<template>
	<view class="content poRel" 
		 :style="'box-sizing: border-box;background-size: 100% 100%;background-image: url('+imageUrl+'/tu8.png);'">
		<view style="height: 125rpx;"></view>
		<view class="mar" style="width: 205rpx;height: 736rpx;"
			:style="'box-sizing: border-box;background-size: 100% 100%;background-image: url('+imageUrl+'/qian.png);'">
			<view style="height: 328rpx;"></view>
			<view class="foSi35 fowe600 dis disAl" style="width: 35rpx;height: 328rpx;line-height: 48rpx;color: #B11918;margin-left: 100rpx;">{{name}}</view>
		</view>
		<view class="mar-top40 foSi40 fowe600 bon mar dis disAl disJuC" @click="zhifu()">解签</view>
	</view>
</template>

<script>
	// #ifdef H5
	var jweixin = require('jweixin-module');
	// #endif
	export default {
		data() {
			return {
				url:this.global.url,
				imageUrl: this.global.imageUrl,
				id: '',
				name: ''
			}
		},
		onLoad(option) {
			this.id = option.id
			this.name = option.name
			this.alioss()
		},
		methods: {
			alioss(){
				var that  = this;
				uni.request({
				    // http://jufeng.tengshuokeji.cn//yonghu/order/order_comments
				    url: that.global.url + '/api/index/alioss',
				    method: "POST",
				    data: {},
				    success(res) {
				        let result = res.data
				        console.log(result)
				        if (result.code == 1) {
				            that.global.ossurl = result.data
							console.log(that.global.ossurl);
							that.ossurl = that.global.ossurl;
				        }
				    }
				})
			},
			zhifu(){
				let that = this 
				uni.showLoading({
					title:'支付中...'
				})
				// #ifdef H5
				var method = 'mp'
				// #endif
				// #ifdef MP-WEIXIN
				var method = 'miniapp'
				// #endif
				uni.request({
					url: that.global.url + '/api/order/order_pay',
					method: 'POST',
					header: {
						"token": uni.getStorageSync('token')
					},
					data: {
						method: method,
						goods_id:that.id,
						type:3,
						openid: uni.getStorageSync('openid')
					},
					success: (res) => {
						let result = res.data
						if (result.code == 1) {
							console.log(result.data)
							if(method == 'mp'){
								jweixin.config({
									debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
									appId: result.data.appId, // 必填，公众号的唯一标识
									timestamp: result.data.timeStamp, // 必填，生成签名的时间戳
									nonceStr: result.data.nonceStr, // 必填，生成签名的随机串
									signature: result.data.paySign, // 必填，签名
									jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表
								})
								jweixin.ready(function() {
									jweixin.chooseWXPay({
										appId: result.data.appId,
										package: result.data.package, //
										timestamp: result.data.timeStamp, // 时间戳
										nonceStr: result.data.nonceStr, // 随机数
										signType: result.data.signType,
										paySign: result.data.paySign, // 签名
										success: function() {
											// 支付成功
											uni.showModal({
												title: '提示',
												content: '支付成功',
												showCancel: false,
												success: function(res) {
													if (res.confirm) {
														uni.redirectTo({
															url: '/pages/wishing/particulars?id='+result.data.order_id,
														});
													}
												}
											})
										},
										cancel: function() {
											// 支付失败
											console.log(res)
											uni.showToast({
												title: '取消支付',
												icon: 'none',
												duration: 2000
											})
										},
										fail: function() {
											// 支付失败
											console.log(res)
											uni.showToast({
												title: '支付失败',
												icon: 'none',
												duration: 2000
											})
										}
									})
								})
							}else{
								uni.requestPayment({
								    provider: 'wxpay',
									timeStamp: result.data.timeStamp,
									nonceStr: result.data.nonceStr,
									package: result.data.package,
									signType: 'MD5',
									paySign: result.data.paySign,
									success: function (res) {
										console.log(res);
										uni.showModal({
											title: '提示',
											content: '支付成功',
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													uni.redirectTo({
														url: '/pages/wishing/particulars?id='+result.data.order_id,
													});
												}
											}
										})
									},
									fail: function (err) {
										console.log('fail:' + JSON.stringify(err));
											// 支付失败
										console.log(err)
										uni.showToast({
											title: '支付失败',
											icon: 'none',
											duration: 2000
										})
									}
								});
							}
						}else if(result.code == 401){
							uni.reLaunch({
								url: '/pages/login/login',
							})
						}else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			tiaozhuan(url){
				uni.redirectTo({
					url: url
				})
				
			}
		}
	}
</script>

<style>
	.content {
		height: 100vh;
	}
	.bon{
		width: 292rpx;
		height: 81rpx;
		color: #B11918;
		background: linear-gradient(to top, #F9AA17, #FEF503);
		border: 8rpx solid #FF7D05;
		border-radius: 50rpx;
	}
</style>
